﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using QiHe.CodeLib;
using QiHe.Office.CompoundDocumentFormat;
using QiHe.Office.Excel;
using Qiniu.FileOp;
using Qiniu.Conf;
using Qiniu;
using Qiniu.RS;
using Qiniu.Auth;
using Qiniu.IO;
using Qiniu.PreFetch;
using Qiniu.RPC;
using Qiniu.RSF;
using Qiniu.IO.Resumable;
using System.Threading;
using System.Net;
using System.IO;
using System.Web;

namespace QiHe.Office.Tool
{
    public partial class Add_Depts: Form
    {

        string log = null;
        int row = 0;
        string file;
        
        DataTable dt = new DataTable();
         DataSet DeptList ;
         DataSet DeptList2;
         DataSet HSPEGNList;
         DataSet HSPEQAdminList;
        string EQflag = "";
        int photoCount = 0;

        public Add_Depts()
        {
            
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;


        
          
        }

        CompoundDocument doc;
     

        void AddTreeNode(TreeNode node, DirectoryEntry entry)
        {
            TreeNode subnode = node.Nodes.Add(entry.Name);
            subnode.Tag = entry;
            foreach (KeyValuePair<string, DirectoryEntry> subentry in entry.Members)
            {
                AddTreeNode(subnode, subentry.Value);
            }
        }

  

        private void LoadExcelSheets()
        {
            byte[] bookdata = doc.GetStreamData("Workbook");
            Workbook book = new Workbook();
            book.Read(new MemoryStream(bookdata));

            //ExtractImages(book, @"C:\Images");

            tabControlSheets.TabPages.Clear();

            foreach (Worksheet sheet in book.Worksheets)
            {
                TabPage sheetPage = new TabPage(sheet.Name);
                DataGridView dgvCells = new DataGridView();
                dgvCells.Dock = DockStyle.Fill;
                dgvCells.RowCount = sheet.MaxRowIndex + 1;
                dgvCells.ColumnCount = sheet.MaxColIndex + 1;

                dt.Clear();
             
                dt.Columns.Add("编号");
                dt.Columns.Add("设备名称");
                dt.Columns.Add("资产编号");
                dt.Columns.Add("出厂编号");
                dt.Columns.Add("设备型号");
                dt.Columns.Add("生产厂商");
                dt.Columns.Add("经销商");
                dt.Columns.Add("购入价格");
                dt.Columns.Add("购入日期");
                dt.Columns.Add("所属科室");
                dt.Columns.Add("设备类别");
                dt.Columns.Add("资产类别");
                dt.Columns.Add("设备负责人");
                dt.Columns.Add("存放地点");
                dt.Columns.Add("存放机房");
                dt.Columns.Add("管理科室");
                dt.Columns.Add("是否计量设备");
                dt.Columns.Add("医院工程师");
                dt.Columns.Add("厂商工程师");
                dt.Columns.Add("售后电话");
                dt.Columns.Add("下一次PM日期");
                dt.Columns.Add("PM周期");
                dt.Columns.Add("备注");

                foreach (KeyValuePair<Pair<int, int>, Cell> cell in sheet.Cells)
                {
                    
                    dgvCells[cell.Key.Right, cell.Key.Left].Value = cell.Value.Value;
                    

                    if (cell.Value.BackColorIndex != 64)
                    {
                        dgvCells[cell.Key.Right, cell.Key.Left].Style.BackColor = cell.Value.BackColor;
                    }
                }


                for (int i = 0; i < dgvCells.RowCount; i++)
                {
                    DataRow dr = dt.NewRow();


                    for (int j = 0; j <5; j++)
                    {
                        String Param = null;
                        if (j == 0) Param = "编号";
                        if (j == 1) Param = "设备名称";
                        if (j == 2) Param = "资产编号";
                        if (j == 3) Param = "出厂编号";
                        if (j == 4) Param = "设备型号";
                        if (j == 5) Param = "生产厂商";
                        if (j == 6) Param = "经销商";
                        if (j == 7) Param = "购入价格";
                        if (j == 8) Param = "购入日期";
                        if (j == 9) Param = "所属科室";
                        if (j == 10) Param = "设备类别";
                        if (j == 11) Param = "资产类别";
                        if (j == 12) Param = "设备负责人";
                        if (j == 13) Param = "存放地点";
                        if (j == 14) Param = "存放机房";
                        if (j == 15) Param = "管理科室";
                        if (j == 16) Param = "是否计量设备";
                        if (j == 17) Param = "医院工程师";
                        if (j == 18) Param = "厂商工程师";
                        if (j == 19) Param = "售后电话";
                        if (j == 20) Param = "下一次PM日期";
                        if (j == 21) Param = "PM周期";
                        if (j == 22) Param = "备注";
                        dr[Param] = dgvCells.Rows[i].Cells[j].Value.ToString();



                    }
                    dt.Rows.Add(dr);


                }

               




                 dataGridView1.DataSource = dt;
                 for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
                 {
                     this.dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                 }

              

                sheetPage.Controls.Add(dataGridView1);
                tabControlSheets.TabPages.Add(sheetPage);
            }
        }

      

        private void exitXToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }





    

        private void button1_Click_1(object sender, EventArgs e)
        {

        }


        void back_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            this.dataGridView1.Rows[row].Cells[0].Value = "已添加";
           label26.Text = "已导入" + row.ToString() + "个部门";
          
            row++;
            if (row < dataGridView1.Rows.Count - 1)
            {
                BackgroundWorker backgroundworker = new BackgroundWorker();
                backgroundworker.DoWork += new DoWorkEventHandler(back_DoWork);
                backgroundworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(back_RunWorkerCompleted);
                backgroundworker.RunWorkerAsync();


            }
            else
                MessageBox.Show("Excel数据已全部添加！");
        }

        void back_DoWork(object sender, DoWorkEventArgs e)
        {
            
            GetFlag F = new GetFlag();
            
            MedEQ.Service1 sop = new MedEQ.Service1();

            log = sop.Add_Dept_WinClient(dt.Rows[row][1].ToString(), dt.Rows[row][0].ToString(), dt.Rows[row][3].ToString().Trim(), dt.Rows[row][2].ToString());
             
      

        
        }






        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EQflag = Guid.NewGuid().ToString();
            row = dataGridView1.CurrentCell.RowIndex;
            

        }

        private void 维修中ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Task ts = new Task();
            ts.Show();


        }




  

      

        

        



     
       

       

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string file = FileSelector.BrowseFile(FileType.All);
                if (file == null) return;
                doc = CompoundDocument.Read(file);

                LoadExcelSheets();
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (row < dataGridView1.Rows.Count - 1)
            {
                BackgroundWorker backgroundworker = new BackgroundWorker();
                backgroundworker.DoWork += new DoWorkEventHandler(back_DoWork);
                backgroundworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(back_RunWorkerCompleted);
                backgroundworker.RunWorkerAsync();


            }
            else
                MessageBox.Show("Excel数据已全部添加！");
        }

       
    }
}